package local.blueheart.data.springdatademo.controller;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

@Controller
public class DetailController {

    @GetMapping("/detail/abc/{docid}")
    public ModelAndView getDoc(@PathVariable("docid") String docId)  {
        System.out.println("docId = " + docId);
        return new ModelAndView("/detail/C7CF6C8D7FBCCB1C4825835400303DB8");
    }

    @GetMapping("/detail/test1")
    public ResponseEntity<InputStreamResource> getAttachment() throws IOException {
        String filePath = "/Users/liuzhenxing/temp/dominotransfer/lib/fsarchive/fsarchivestore_B5T9QH/nsf/C7CF6C8D7FBCCB1C4825835400303DB8.mht";
        FileSystemResource file = new FileSystemResource(filePath);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("inline; filename=\"%s\"", file.getFilename()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("message/rfc822"))
                .body(new InputStreamResource(file.getInputStream()));
    }


}
